<template>
  <div>
    <div class="row">
      <div class="col-md-6">
        <p>
          <!--  新增按钮  -->
          <button class="btn btn-white btn-default btn-round" @click="add1()">
            <i class="ace-icon fa fa-edit"></i>
            新增一级
          </button>&nbsp;

          <!--  刷新按钮  -->
          <button class="btn btn-white btn-default btn-round" @click="all()">
            <i class="ace-icon fa fa-refresh"></i>
            刷新
          </button>
        </p>

        <!--  一级菜单表格数据  -->
        <table id="level1-table" class="table  table-bordered table-hover">
          <thead>
          <tr>
            <th>ID</th>
            <th>名称</th>
            <th>顺序</th>
            <th>操作</th>
          </tr>
          </thead>

          <tbody>
          <tr v-for="category in level1" @click="onclickLevel1(category)"
              v-bind:class="{'active' : category.id === active.id}">
            <td>{{ category.id }}</td>
            <td>{{ category.name }}</td>
            <td>{{ category.sort }}</td>
            <td>
              <div class="hidden-sm hidden-xs btn-group">

                <button class="btn btn-white btn-xs btn-info btn-round" @click="edit(category)">
                  <i class="ace-icon fa fa-pencil bigger-160"></i>
                  编辑
                </button>&nbsp;

                <button class="btn btn-white btn-xs btn-danger btn-round" @click="del(category.id)">
                  <i class="ace-icon fa fa-trash-o bigger-160"></i>
                  删除
                </button>

              </div>
            </td>
          </tr>
          </tbody>
        </table>
      </div>

      <div class="col-md-6">
        <p>
          <!--  新增按钮  -->
          <button class="btn btn-white btn-default btn-round" @click="add2()">
            <i class="ace-icon fa fa-edit"></i>
            新增二级
          </button>
        </p>

        <table id="level2-table" class="table  table-bordered table-hover">
          <thead>
          <tr>
            <th>ID</th>
            <th>名称</th>
            <th>顺序</th>
            <th>操作</th>
          </tr>
          </thead>

          <tbody>
          <tr v-for="category in level2">
            <td>{{ category.id }}</td>
            <td>{{ category.name }}</td>
            <td>{{ category.sort }}</td>
            <td>
              <div class="hidden-sm hidden-xs btn-group">

                <button class="btn btn-white btn-xs btn-info btn-round" @click="edit(category)">
                  <i class="ace-icon fa fa-pencil bigger-160"></i>
                  编辑
                </button>&nbsp;

                <button class="btn btn-white btn-xs btn-danger btn-round" @click="del(category.id)">
                  <i class="ace-icon fa fa-trash-o bigger-160"></i>
                  删除
                </button>

              </div>
            </td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- PAGE CONTENT BEGINS -->
    <!--  模态框  -->
    <div id="form-modal" class="modal fade" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title">新增</h4>
          </div>
          <!--  模态框主体内容  -->
          <div class="modal-body">

            <!--  模态框中的表单  -->
            <form class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label">父分类</label>
                <div class="col-sm-10">
                  <p class="form-control-static">{{ active.name || "无" }}</p>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">名称</label>
                <div class="col-sm-10">
                  <input type="text" v-model="category.name" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">顺序</label>
                <div class="col-sm-10">
                  <input type="text" v-model="category.sort" class="form-control">
                </div>
              </div>
            </form>
          </div>

          <!--  模态框底部内容  -->
          <div class="modal-footer">
            <!--  data-dismiss的作用为关闭模态框的事件  -->
            <button type="button" class="btn btn-white btn-lg btn-danger btn-round" data-dismiss="modal">
              <i class="ace-icon fa fa-times red"></i>
              取消
            </button>
            <button type="button" class="btn btn-white btn-lg btn-info btn-round" @click="save()">
              <i class="ace-icon fa fa-save blue"></i>
              保存
            </button>
          </div>

        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
    <!-- PAGE CONTENT ENDS -->

  </div>


</template>

<script>
export default {
  name: "business-category",
  mounted: function () {
    let _this = this;
    _this.all();
  },
  data: function () {
    return {
      // 用来映射表单的数据
      category: {},
      categorys: [],
      // 一级分类
      level1: [],
      // 二级分类
      level2: [],
      // 表示当前点击的一级标题行
      active: {},
    }
  },
  methods: {
    all() {
      let _this = this;
      // 调用显示延迟框的方法
      Loading.show();
      _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/category/all').then((result) => {
        // 关闭延迟框的显示
        Loading.hide();
        let response = result.data;
        _this.categorys = response.content;

        // 在格式化之前先将level1清空，防止出现一直叠加一级标题的情况
        _this.level1 = [];
        // 将所有记录格式化成树形结构
        for (let i = 0; i < _this.categorys.length; i++) {
          let c = _this.categorys[i];
          // 判断是否为父ID
          if (c.parent === '00000000') {
            // 此时c为一级分类
            _this.level1.push(c);

            // 寻找此时一级分类下的二级分类
            for (let j = 0; j < _this.categorys.length; j++) {
              let child = _this.categorys[j];
              // 判断是否为父ID
              if (child.parent === c.id) {
                // 此时child为c的子标题
                // 只有当c的children数组为空的时候才去初始化children数组
                if (Tool.isEmpty(c.children)) {
                  c.children = [];
                }

                c.children.push(child);
              }
            }
          }
        }

        _this.level2 = [];
        // 对当前一级分类中选中的表格触发一次点击事件，以刷新二级菜单列表
        // 注意：界面的渲染需要等待vue绑定好变量以后再做，所以加延时100ms
        setTimeout(function () {
          $("tr.active").trigger("click");
        }, 100);
      })
    },

    // 点击新增一级
    add1() {
      let _this = this;
      _this.active = {};
      _this.level2 = {};
      _this.category = {
        parent: "00000000"
      };
      $("#form-modal").modal("show");
    },

    // 点击新增二级
    add2() {
      let _this = this;
      if (Tool.isEmpty(_this.active)) {
        Toast.warning("请先选择一级分类！");
        return;
      }
      _this.category = {
        parent: _this.active.id
      };
      $("#form-modal").modal("show");
    },

    // 当点击编辑按钮时触发的事件
    edit(category) {
      let _this = this;
      _this.category = $.extend({}, category);
      $("#form-modal").modal("show");
    },

    // 编辑和新增的调用方法
    save() {
      let _this = this;

      // 保存校验，加1!=1是为了解决第一个||符号的问题
      if (1 != 1
          || !Validator.require(_this.category.parent, "父id")
          || !Validator.require(_this.category.name, "名称")
          || !Validator.length(_this.category.name, "名称", 1, 50)
      ) {
        return;
      }

      // 调用显示延迟框的方法
      Loading.show();
      _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/category/save', _this.category).then((result) => {
        // 关闭延迟框的显示
        Loading.hide();
        // 用response来代替result.data
        let response = result.data;
        if (response.success) {
          $("#form-modal").modal("hide");
          _this.all();
          Toast.success("编辑成功！")
        } else {
          Toast.warning(response.message)
        }
      })
    },
    // 删除的调用方法
    del(id) {
      let _this = this;
      Confirm.show("删除后不可恢复，确认删除吗？", function () {
        // 调用显示延迟框的方法
        Loading.show();
        _this.$ajax.delete(process.env.VUE_APP_SERVER + '/business/admin/category/delete/' + id).then((result) => {
          let response = result.data;
          // 当删除成功时
          if (response.success) {
            // 刷新列表
            _this.all();
            // 调用Toast组件的成功效果
            Toast.success("删除成功！")
          } // end if

        }) // end then
      })
    },

    onclickLevel1(category) {
      let _this = this;
      // 当前激活的数据放到active中
      _this.active = category;
      // 使level2的值为category的children数组
      _this.level2 = category.children;
    }

  } // methods
};
</script>

<style scoped>
.active td {
  background-color: #bbe2fd !important;
}
</style>
